﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.EzAppApi.Utility;
using System.Diagnostics;
using EzDesk.Core.Model;

namespace EzDesk.Apps.ServiceImpl.Repositories.Mock
{
    public class Mock_Storage<T>
        where T : IModel
    {

        public IEzApiClient ApiClient = new Mock_EzApiClient();

        public static Guid existedTestProjectId = new Guid("AD8CBE58-9A67-4632-AB4C-6BDE5AF66CB2");
        public static Guid testProjectId1 = new Guid("8F5BC301-9F1E-424D-9450-557914B8DFA7");
        public static Guid testProjectId2 = new Guid("6F94A5CC-75DE-4501-A3B0-C65D1FAEA412");

        public Dictionary<string, Guid> SampleUsers =
            new Dictionary<string, Guid>()
            {
                {"卢春叶", new Guid("ce36c495-38cc-4b7e-875e-364ff8e087b5")},
                {"杨裕欣", new Guid("0c9d7c0c-04a5-4ae2-97a1-d7bbc38f5c66")},
                {"龚伟", new Guid("19f1c421-280f-4935-9980-e469175461c2")},
                {"Karin", new Guid("877c3323-17a7-42d1-9d75-402ffc894e63")},
            };

        protected static List<T> innerStorage = new List<T>();

        public void SetInnerStorage(IList<T> data)
        {
            innerStorage = (List<T>) data;
        }

        public void ClearAll()
        {
            innerStorage.Clear();
        }
        
        public Guid Add(T model, bool saveChanges)
        {
            if (!saveChanges)
            {
                Debug.WriteLine("Mock不支持延迟保存");
            }
            innerStorage.Add(model);
            return model.Id;
        }

        public T GetById(Guid id)
        {
            return innerStorage.Where(p => p.Id == id).FirstOrDefault();
        }

        public IQueryable<T> GetAll()
        {
            return innerStorage.AsQueryable();
        }

        public void Delete(Guid id, bool saveChanges)
        {
            if (!saveChanges)
            {
                Debug.WriteLine("Mock不支持延迟保存");
            }
            innerStorage.Remove(GetById(id));
        }

        public void Update(T model, bool saveChanges)
        {
            if (!saveChanges)
            {
                Debug.WriteLine("Mock不支持延迟保存");
            }
            innerStorage[innerStorage.IndexOf(GetById(model.Id))] = model;
        }

        public void SaveChanges()
        {
            Debug.WriteLine("Mock不支持延迟保存");
        }
    }
}
